package com.example.tianling.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.tianling.admin.service.ActivateService;
import com.example.tianling.admin.service.UserDataService;
import com.example.tianling.admin.vo.AdminVo;
import com.example.tianling.admin.vo.UserDataVo;
import com.example.tianling.admin.vo.userDetailDataVo;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.annotation.LoginUser;
import com.example.tianling.common.utils.R;
import com.example.tianling.dao.UserMapper;
import com.example.tianling.entity.ArticleEntity;
import com.example.tianling.entity.CommentEntity;
import com.example.tianling.entity.UserEntity;
import com.example.tianling.service.ArticleService;
import com.example.tianling.service.CommentService;
import com.example.tianling.service.UserService;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @className: UserController
 * @author: Kyle
 * @date: 2023/8/3 11:09
 **/
@Controller
public class AdminUserController {

    @Autowired
    private UserDataService userDataService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private ActivateService activateService;
    //注册用户分析，活跃总数，每个地区的注册人数
    @Login
    @PostMapping("/admin/index.do")
    @ResponseBody
    public R adminUser(@LoginUser UserEntity user){
        return R.success(new AdminVo(user.getUserName(),user.getUserImg()));
    }
    @Login
    @PostMapping("/admin/user/analysis.do")
    @ResponseBody
    public R analysis(){
        List<Map<String, Object>> sum = userDataService.sumUser();
        List<Map<String, Object>> countByArea = userService.getCountByArea();
        return R.success(new UserDataVo(sum,countByArea));
    }

    @Login
    @PostMapping("/admin/user/activate.do")
    @ResponseBody
    public R activateUser(@RequestBody String params){
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String startTime = paramsJSONObject.getString("startTime");
        String endTime = paramsJSONObject.getString("endTime");
        List result = activateService.findActivate(startTime,endTime);
        return R.success(result);
    }

    @Login
    //搜索用户
    @PostMapping("/admin/user/find_user.do")
    @ResponseBody
    public R findUser(@RequestBody String params){
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String type = paramsJSONObject.getString("type");
        String keyword = paramsJSONObject.getString("keyword");
        if(Integer.valueOf(type)==2){

            UserEntity userEntity= userService.getById(keyword);
            List<UserEntity> ls = new ArrayList<>();
            ls.add(userEntity);
            return R.success(ls);
        }
        else {
            return R.success(userDataService.findUserByName(keyword));
        }

    }
    //用户资料查看 (帖子、券数、充值记录、评论记录)
    //查看帖子

    @Login
    @PostMapping("/admin/user/find_user_article.do")
    @ResponseBody
    public R findUserArticle(@RequestBody UserEntity user){
        System.out.println(user);
        List<ArticleEntity> myArticle = articleService.getMyArticle(user.getUserId());
        List<CommentEntity> comment = commentService.findComment(user);
        return R.success(new userDetailDataVo(myArticle,comment));
    }

    //删除评论
    @Login
    @PostMapping("/admin/user/delete_comment.do")
    @ResponseBody
    public R deleteComment(@RequestBody CommentEntity commentEntity){
        int result = commentService.deleteComment(String.valueOf(commentEntity.getCommentId()));
        if(result == 1) return R.success();
        else return R.error(409,"删除失败");
    }
    //删除用户
    @Login
    @PostMapping("/admin/user/delete_user.do")
    @ResponseBody
    public R deleteUser(@RequestBody UserEntity user){
        int result = userMapper.deleteById(user.getUserId());
        if(result == 1) return R.success();
        else return R.error(409,"删除失败");
    }
}
